<?php

class Pages extends Admin_Controller {

	function __construct()
	{
		parent::__construct();
		
		$this->load->library('pages_lib');
	}
	

	
	
	public function index()
	{
		$this->aPageJs[] = array(
							'combine_js' => true,
							'base_href' => '/media/js',
							'js' => array('admin_pages'),
							'version' => '1.1'
		);	
		
		$this->aPageCss[] = array(
							'combine_css' => true,
							'base_href' => '/media/css',
							'css' => array('admin_pages'),
							'media' => 'all',
							'version' => '1.1'
		);
		
		$qPages = $this->pages_lib->get_page_hierarchy(array('page_name' => NULL), array('page_deleted' => 'N', 'page_status' => 'LI'));
		
		$sPages = $this->pages_lib->build_page_hierarchy($qPages, 'ul', 'page_container', 'li', 'page', true, '#');
		
		$this->aData['sPages'] = $sPages;
		
		$this->load->view('platform/'.$this->sPlatform.'/admin/pages/index', $this->aData);
	}
	
	public function add_page()
	{
		$this->edit_page();
	}
	
	public function edit_page($iPageDataId = 0)
	{
		$this->aPageJs[] = array(
							'combine_js' => false,
							'base_href' => '/assets/js/tiny_mce',
							'js' => array('tiny_mce_src')
		);
		
		$this->aPageJs[] = array(
							'combine_js' => true,
							'base_href' => '/media/js',
							'js' => array('admin_edit_page', 'tiny_mce'),
							'version' => '1.1'
		);	
		
		$this->aPageCss[] = array(
							'combine_css' => true,
							'base_href' => '/media/css',
							'css' => array('admin_edit_page'),
							'media' => 'all',
							'version' => '1.1'
		);
		
		$this->load->model('db/views/pages_extended', 'vw_pages_extended');
		$this->vw_pages_extended->page_status = 'LI';
		$this->vw_pages_extended->page_deleted = 'N';
		$this->vw_pages_extended->page_archived = 'N';
		$aOptions = array();
		$aOptions[] = array('group_by' => array('page_id'));
		$qPages = $this->vw_pages_extended->get($aOptions);
		$this->aData['qPages'] = $qPages;
		
		$this->load->model('db/views/pages_extended_content', 'vw_pages_extended_content');
		$this->vw_pages_extended_content->page_data_id = $iPageDataId;
		$qPage = $this->vw_pages_extended_content->get(1);
		$oPage = $qPage->num_rows() === 1 ? $qPage->row(0) : $this->vw_pages_extended_content->get_object();
		$this->aData['oPage'] = $oPage;
		
		/* MENUS */
		$this->load->model('db/menus', 'menus');
		
		$this->menus->menu_active = 'Y';
		
		$qMenus = $this->menus->get();
		$iMenus = $qMenus->num_rows();
		
		$this->load->model('db/page_data_menus', 'page_data_menus');
		
		for ($i = 0; $i < $iMenus; $i++)
		{
			$this->page_data_menus->clear();
			$this->page_data_menus->page_data_id = $iPageDataId;
			$this->page_data_menus->menu_id = $qMenus->row($i)->menu_id;
			$this->page_data_menus->page_data_menu_type = 'H';
			
			$qMenuHas = $this->page_data_menus->get(array(), 1);
			
			$qMenus->row($i)->menu_has = ($qMenuHas->num_rows() === 1);	


			$this->page_data_menus->clear();
			$this->page_data_menus->page_data_id = $iPageDataId;
			$this->page_data_menus->menu_id = $qMenus->row($i)->menu_id;
			$this->page_data_menus->page_data_menu_type = 'A';
			
			$qMenuAppears = $this->page_data_menus->get(array(), 1);
			
			$qMenus->row($i)->menu_appears = ($qMenuAppears->num_rows() === 1);	

		}
		
		$this->aData['qMenus'] = $qMenus;
		
		/* COMPONENTS */
		$this->load->model('db/site_components', 'site_components');
		$this->site_components->component_status = 'LI';
		$qComponents = $this->site_components->get();
		
		$this->aData['qComponents'] = $qComponents;
		
		$this->load->model('db/page_data_components', 'page_data_components');
		$this->page_data_components->page_data_id = $iPageDataId;
		$qPageDataComponents = $this->page_data_components->get();
		$aPageDataComponents = array();
		
		for($i = 0; $i < $qPageDataComponents->num_rows(); $i++)
		{
			if(!isset($aPageDataComponents[$qPageDataComponents->row($i)->component_position]))
			{
				$aPageDataComponents[$qPageDataComponents->row($i)->component_position] = array();
			}
			
			$aPageDataComponents[$qPageDataComponents->row($i)->component_position][] = $qPageDataComponents->row($i)->component_id;
		}
		
		$this->aData['aPageDataComponents'] = $aPageDataComponents;
		
		/* BANNERS */
		$this->load->model('db/baa_banners', 'baa_banners');
		$this->baa_banners->banner_status = 'LI';
		$qBanners = $this->baa_banners->get();
		
		$this->aData['qBanners'] = $qBanners;
		
		$this->load->model('db/page_data_banners', 'page_data_banners');
		$this->page_data_banners->page_data_id = $iPageDataId;
		$qPageDataBanner = $this->page_data_banners->get(1);
		$iPageDataBanner = $qPageDataBanner->num_rows() === 1 ? $qPageDataBanner->row(0)->banner_id : 0;
		
		$this->aData['iPageDataBanner'] = $iPageDataBanner;
		
		$this->load->view('platform/'.$this->sPlatform.'/admin/pages/edit', $this->aData);
	}
}

?>